package com.baozun.houji.me.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.InputFilter
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import com.baozhun.mall.common.base.BaseActivity
import com.baozhun.mall.common.ext.showToast
import com.baozhun.mall.common.util.Constants
import com.baozun.houji.me.R
import com.baozun.houji.me.databinding.ActivityUpdateNickNameBinding
import com.hjq.bar.OnTitleBarListener
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import java.util.regex.Matcher
import java.util.regex.Pattern


/**
 * @Title：更新昵称页面
 * @Description：
 * @Author：xuefeng.zhu
 * 
 * @Since：2022/11/17
 * @Version：1.0.0
 */
class UpdateNickNameActivity : BaseActivity<BaseViewModel, ActivityUpdateNickNameBinding>(), OnTitleBarListener, TextWatcher {

    val mCurrentNickName by lazy { intent.getStringExtra(Constants.Common.BUNDLE_NAME).toString() }

    companion object {
        fun startForResult(context: Activity, requestCode: Int, currentNickName: String) {
            var intent = Intent(context, UpdateNickNameActivity::class.java)
            intent.putExtra(Constants.Common.BUNDLE_NAME, currentNickName)
            context.startActivityForResult(intent, requestCode)
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDataBinding.etNickName.setText(mCurrentNickName)
        setEditTextInhibitInputSpeChat(mDataBinding.etNickName)
        mDataBinding.etNickName.addTextChangedListener(this)
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {
            R.id.tv_save -> {
                setResult(Activity.RESULT_OK, Intent().apply {
                    putExtra(Constants.Common.BUNDLE_NAME, mDataBinding.etNickName.text.toString())
                })
                showToast(com.baozhun.mall.res.R.string.nick_name_update_success)
                finish()

            }
        }
    }


    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        //允许输入空格
        if (s.toString().contains(" ")) {
            val str = s.toString().split(" ")
            var str1 = ""
            for (i in str.indices) {
                str1 += str[i]
            }
            mDataBinding.etNickName.setText(str1)
            mDataBinding.etNickName.setSelection(start)
        }

    }

    override fun afterTextChanged(s: Editable?) {
    }

    /**
     * 禁止EditText输入特殊字符
     *
     * @param editText
     */
    private fun setEditTextInhibitInputSpeChat(editText: EditText) {
        val filter = InputFilter { source, start, end, dest, dstart, dend ->
            //过滤特殊字符
            val speChat = "[〔〕｛《｝》「」÷`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？1234567890]"
            val pattern: Pattern = Pattern.compile(speChat)
            val matcher: Matcher = pattern.matcher(source.toString())
            //过滤表情
            var emoji = Pattern.compile(
                "[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                Pattern.CASE_INSENSITIVE or Pattern.UNICODE_CASE
            )
            var emojiMatcher = emoji.matcher(source)
            if (matcher.find() || emojiMatcher.find()) "" else null

        }
        editText.filters = arrayOf(filter, InputFilter.LengthFilter(10))
    }
}